<template>
    <div class="app-container">
        <el-dialog v-model="props.visible" title="物料库明细选择" width="71%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="auto" v-show="showSearch">
                <el-row>
<!--                    <el-form-item label="入库单编码" prop="inCode">-->
<!--                        <el-input v-model.trim="queryParams.inCode" clearable style="width: 150px;" placeholder="请输入库单编码"-->
<!--                            @keyup.enter="handleQuery" />-->
<!--                    </el-form-item>-->
<!--                    <el-form-item label="供应商名称" prop="supplierName">-->
<!--                        <el-input v-model.trim="queryParams.supplierName" clearable style="width: 150px;"-->
<!--                            placeholder="请输入供应商名称" @keyup.enter="handleQuery" />-->
<!--                    </el-form-item>-->
                    <el-form-item label="物料分类" prop="className">
                        <el-input v-model.trim="queryParams.className" clearable style="width: 150px;"
                                  placeholder="请输入物料分类" @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="物料名称" prop="materialName">
                        <el-input v-model.trim="queryParams.materialName" clearable style="width: 150px;"
                            placeholder="请输入物料名称" @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="物料编码" prop="materialCode">
                        <el-input v-model.trim="queryParams.materialCode" clearable style="width: 150px;"
                                  placeholder="请输入物料编码" @keyup.enter="handleQuery" />
                    </el-form-item>
                    
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-row>
            </el-form>
            <div>
                <el-button style="margin-bottom: 5px;" type="primary" plain @click="handleSubmit"
                    :disabled="multiple">确认</el-button>
                <el-table ref="dataListRef" v-loading="loading" :data="dataList" stripe row-key="id" border style="cursor: pointer;"
                    @selection-change="handleSelectionChange">
                    <el-table-column type="selection" :reserve-selection="true" align="center" width="65" />
<!--                    <el-table-column label="入库单编码" min-width="150" align="center" prop="inCode"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                        <template v-slot="{ row }">-->
<!--                            <el-button type="primary" text>{{ row.inCode }}</el-button>-->
<!--                        </template>-->
<!--                    </el-table-column>-->
<!--                    <el-table-column label="入库日期" min-width="150" align="center" prop="inDate"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                    </el-table-column>-->
<!--                    <el-table-column label="供应商编码" min-width="180" align="center" prop="supplierCode"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                        <template v-slot="{ row }">-->
<!--                            <el-button type="primary" text>{{ row.supplierCode }}</el-button>-->
<!--                        </template>-->
<!--                    </el-table-column>-->
<!--                    <el-table-column label="供应商名称" min-width="150" align="center" prop="supplierName"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                    </el-table-column>-->
                    <el-table-column label="物料分类" min-width="150" align="center" prop="className"
                        :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="物料编码" min-width="200" align="center" prop="materialCode"
                        :show-overflow-tooltip="true">
                        <template v-slot="{ row }">
                            <el-button type="primary" text>{{ row.materialCode }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="物料名称" min-width="150" align="center" prop="materialName"
                        :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="规格型号" min-width="120" align="center" prop="specs"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="单位" min-width="70" align="center" prop="unit" :show-overflow-tooltip="true">
                        <template #default="scope">
                            <dict-tag :options="mes_material_unit" :value="scope.row.unit" />
                        </template></el-table-column>
                    <!-- 20240313 库存数量之前的字段stockNum-->
                    <el-table-column label="库存数量" min-width="90" align="center" prop="quantity">
                    </el-table-column>
                    <el-table-column min-width="100" label="单价" align="center" prop="price">
                    </el-table-column>
                    <el-table-column width="100" label="金额" align="center" prop="amount" :show-overflow-tooltip="true" />
                    <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
                </el-table>
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>


<script setup name="searchMaterialReceipt">
import { detailMaterialin } from '@/api/storage/materialReceipt.js'
const { proxy } = getCurrentInstance();
const emit = defineEmits(['changeUpdate'])
const { mes_material_unit, } = proxy.useDict('mes_material_unit',);
const loading = ref(true);
const dataList = ref([])
const selectRow = ref([])
const multiple = ref(true)
const showSearch = ref(true);
const router = useRouter()
const dataListRef =ref(null)
const total = ref(0);
const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },
    receiptList: {
        type: Array,
        default: () => { [] }
    }

})
watch(() => props.visible, (val) => {
    if (val) {
        handleQuery()
        nextTick(() => {
            dataListRef.value.clearSelection()
            if (props.receiptList.length > 0) {
                props.receiptList.forEach(item => {
                    dataListRef.value.toggleRowSelection(item, true)
                })
            }
        })
    }
})
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: 1
    },
});


const { queryParams } = toRefs(data);
const { mes_paper_material, mes_paper_specs, mes_paper_brand
} = proxy.useDict("mes_paper_material", "mes_paper_specs", "mes_paper_brand");

const getList = () => {
    loading.value = true
    detailMaterialin(queryParams.value).then(res => {
        loading.value = false
        dataList.value = res.rows
        total.value = res.total
    }).catch(err => {
        loading.value = false
    })
}
const handleCancel = () => {
    proxy.resetForm("queryRef");
    emit('cancel')
}
// 选中
const handleSelectionChange = (row) => {
    console.log(row, 'row');
    row.forEach(item => {
        item.total = Number(item.quantity)
    })
    selectRow.value = row
    multiple.value = !row.length
}
const handleSubmit = () => {
    emit('changeUpdate', selectRow.value)
}
// const handleCellClick = (row) => {
//     proxy.$modal.confirm('是否确认选择「' + row.inCode + '」').then(function () {
//         emit('changeUpdate', row)
//     }).catch(() => { });
// }
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};
// getList()
</script>
<style lang="scss" scoped></style>